package servlets;


import java.io.IOException;
//import java.util.ArrayList;

//import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.EmailDTO;

import services.EmailServiceImpl;

import common.FileManipulation;
import controllers.Newsletter_Controller;

/**
 * @author 	Sami Diab 
 * Servlet implementation class Newsletter
 */
//@WebServlet("/Newsletter")
@WebServlet(name="Newsletter", urlPatterns={"/JSP/Sami/Admin/Newsletter"})
public class Newsletter extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
    	String txtSubject= request.getParameter("txtSubject");
    	String txtDisplayName= request.getParameter("txtDisplayName");
    	String txtTitle= request.getParameter("txtTitle");
    	String txtContent= request.getParameter("txtContent");
    	String ddlTemplates= request.getParameter("ddlTemplates");
    	String status;
    	boolean isSent;
    	String email;
    	
    	//Get the content of the template file
    /*	String htmTemplate = FileManipulation.readFile( request.getServletContext().getRealPath("/Templates/") + "\\"+ ddlTemplates);
    	htmTemplate = htmTemplate.replace("[Title]", txtTitle);
    	htmTemplate = htmTemplate.replace("[Content]", txtContent);
    	*/
    	//Get the list of emails.
		/*ArrayList<String> listUsers = new ArrayList<String>();
		int emailListCount = (Integer) request.getSession().getAttribute("EmailListCount");
		
		for (int i = 0; i < emailListCount; i++ )
		{
			email = request.getParameter("chb-key" + i);
			if(email != null) listUsers.add(email);
		}*/
		
    	//int emailListCount = (Integer) request.getSession().getAttribute("EmailListCount");
    	String[] emailAdresses = request.getParameterValues("emailAddresses");
    	
		if(emailAdresses != null && emailAdresses.length > 0){
			
			//Send the the email to there destinations
			Newsletter_Controller newsletter = new Newsletter_Controller();
		  //  isSent = newsletter.sendEmails(emailAdresses, txtDisplayName, txtSubject, htmTemplate); //"465" 			
			isSent = true;
			if(isSent == true)
			{
				status = "The email has been successfully sent.";
				
			}else
			{
				status = "The email has not been sent, please try again.";
			}
		}
		else
		{
			status = "The email has not been sent since no destination has been checked.";
		}
			
		//set the error message request variable  
	    request.setAttribute("status",status);  
	    //important forward it back to the login page again. 
    	request.getRequestDispatcher("Newsletter.jsp").forward(request, response);
		
	}
}
